package com.yzt.zhuju.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.yzt.zhuju.IService.chatroom.IChatRoomService;
import com.yzt.zhuju.arouter.RouterMain;
import com.yzt.zhuju.network.entity.call.AgoraCallMsg;
import com.yzt.zhuju.utils.UrlUtil;

/**
 * author : bob
 * date   : 2022/12/1
 * desc   :
 */
public class NotifyBroadCastReceiver extends BroadcastReceiver {

    public static final String KEY_NOTIFY_TYPE = "notify_type";
    public static final String KEY_EVENT_TYPE = "event_type";

    public static final String TYPE_AROUTER = "type_arouter";
    public static final String TYPE_OTHER = "type_other";

    public static final String BUNDLE_AROUTER_JSON = "type_arouter_JSON";

    public static final String TYPE_OTHER_EVENT_CHAT_ROOM = "open_chat_room";
    public static final String TYPE_OTHER_EVENT_MAIN_CALL = "open_main_call";

    public NotifyBroadCastReceiver() {
        super();
    }

    @Override
    public IBinder peekService(Context myContext, Intent service) {
        return super.peekService(myContext, service);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        String type = "";
        try {
            if (bundle != null) {
                type = bundle.getString(KEY_NOTIFY_TYPE);
            }
            if (TextUtils.isEmpty(type)) {
                type = "";
            }
        } catch (Exception ignored) {

        }
        switch (type) {
            case TYPE_AROUTER:
                if (null == bundle) {
                    return;
                }
                //定义好的跳转规则
                String arountJson = bundle.getString(BUNDLE_AROUTER_JSON);
                UrlUtil.turnToARoutePage4JO(arountJson);
                break;
            case TYPE_OTHER: {
                if (null == bundle) {
                    return;
                }
                String event = bundle.getString(KEY_EVENT_TYPE);
                switch (event) {
                    case TYPE_OTHER_EVENT_CHAT_ROOM:
                        String roomId = intent.getExtras().getString("roomdId");
                        ARouter.getInstance().navigation(IChatRoomService.class).enterChatRoom(roomId);
                        break;
                    case TYPE_OTHER_EVENT_MAIN_CALL:
                        AgoraCallMsg callMsg = (AgoraCallMsg) bundle.getSerializable("AgoraCallMsg");
                        RouterMain.MainActivity(callMsg);
                        break;
                    default:
                        break;
                }
            }
            break;
            default:
                RouterMain.MainActivity();
                break;
        }
    }
}
